package com.dc.toolkit.guava.base;

import com.google.common.base.Splitter;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;
import java.util.Map;

/**
 * Description: Guava_Splitter测试类
 * @author: gdc
 * @date: 2021/4/17
 * @version 1.0
 */
@Slf4j
public class SplitterTest {

    /**
     * Splitter.on().splitToList()          将字符串拆分为List
     */
    @Test
    public void testSplit_On_Split() {
        List<String> result = Splitter.on("|").splitToList("hello|world");
        Assert.assertTrue(result.size() == 2);
        Assert.assertEquals(result.get(0), "hello");
        Assert.assertEquals(result.get(1), "world");
    }

    /**
     * Splitter.on().omitEmptyStrings().splitToList()          将字符串拆分为List(忽略空的情况)
     */
    @Test
    public void testSplit_On_Split_OmitEmpty() {
        String target = "hello|world|||";

        List<String> result = Splitter.on("|").splitToList(target);
        Assert.assertEquals(5, result.size());

        List<String> result2 = Splitter.on("|").omitEmptyStrings().splitToList(target);
        Assert.assertEquals(result2.size(), 2);
    }

    /**
     * Splitter.on().trimResults().omitEmptyStrings().splitToList()          将字符串拆分为List(忽略空的情况 && 并去除字符串两边的空格)
     */
    @Test
    public void testSplit_On_Split_OmitEmpty_TrimResult() {
        String target = "hello | world| ||";

        List<String> result = Splitter.on("|").omitEmptyStrings().splitToList(target);
        Assert.assertEquals(result.size(), 3);
        Assert.assertEquals(result.get(0), "hello ");


        List<String> result2 = Splitter.on("|").trimResults().omitEmptyStrings().splitToList(target);
        Assert.assertEquals(result2.size(), 2);
        Assert.assertEquals(result2.get(0), "hello");
    }

    /**
     * Splitter.fixedLength()               以固定长度N,进行截取字符串
     */
    @Test
    public void testSplit_FixLength(){
        List<String> result = Splitter.fixedLength(4).splitToList("aaaabbbbccccddd");
        Assert.assertEquals(result.get(0), "aaaa");
        Assert.assertEquals(result.get(3), "ddd");
    }

    /**
     * Splitter.on().limit(3)               截取后限制结果数量
     */
    @Test
    public void testSplit_On_SplitLimit(){
        List<String> result = Splitter.on("#").limit(3).splitToList("hello#world#java#guava#python");
        Assert.assertEquals(result.size(), 3);
        Assert.assertEquals(result.get(0), "hello");
    }

    /**
     * Splitter.onPattern()                 通过正则表达式进行截取
     */
    @Test
    public void testSplit_On_PatternString(){
        List<String> result = Splitter.onPattern("\\#").trimResults().omitEmptyStrings().splitToList("hello #  world #  java#guava#python");
        Assert.assertEquals(result.get(0), "hello");
    }

    /**
     * Splitter.on().withKeyValueSeparator().split()                 截取数据转换为Map
     */
    @Test
    public void testSplit_On_SplitToMap(){
        Map<String, String> result = Splitter.onPattern("\\#")
                .trimResults()
                .omitEmptyStrings()
                .withKeyValueSeparator("=")
                .split("hello=HELLO #  world=WORLD");
        Assert.assertEquals(result.size(), 2);
        Assert.assertEquals(result.get("hello"), "HELLO");
    }

}
